package com.ruoyi.stuinfo.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.stuinfo.domain.Classper;
import com.ruoyi.stuinfo.domain.HomeworkStatsVO;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.stuinfo.domain.Ass;
import com.ruoyi.stuinfo.service.IAssService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 作业情况Controller
 *
 * @author zc
 * @date 2025-05-16
 */
@RestController
@RequestMapping("/stuinfo/ass")
public class AssController extends BaseController
{
    @Autowired
    private IAssService assService;

    /**
     * 查询作业情况列表
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:ass:list')")
    @GetMapping("/list")
    public TableDataInfo list(Ass ass)
    {

        String teacherId=getUsername();
        boolean hasH = teacherId.contains("H");
        if("admin".equals(teacherId))
        {
            //管理员
            startPage();
            List<Ass> list = assService.selectAssList(ass);
            return getDataTable(list);
        }
        else
        {
            //班主任
            teacherId=teacherId.replace("H", "");
            startPage();
            List<Ass> list = assService.selectAssList_handteacher(teacherId);
            return getDataTable(list);
        }
    }

    /**
     * 导出作业情况列表
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:ass:export')")
    @Log(title = "作业情况", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Ass ass)
    {
        List<Ass> list = assService.selectAssList(ass);
        ExcelUtil<Ass> util = new ExcelUtil<Ass>(Ass.class);
        util.exportExcel(response, list, "作业情况数据");
    }

    /**
     * 获取作业情况详细信息
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:ass:query')")
    @GetMapping(value = "/{submissionId}")
    public AjaxResult getInfo(@PathVariable("submissionId") Long submissionId)
    {
        return success(assService.selectAssBySubmissionId(submissionId));
    }

    /**
     * 新增作业情况
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:ass:add')")
    @Log(title = "作业情况", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Ass ass)
    {
        return toAjax(assService.insertAss(ass));
    }

    /**
     * 修改作业情况
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:ass:edit')")
    @Log(title = "作业情况", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Ass ass)
    {
        return toAjax(assService.updateAss(ass));
    }

    /**
     * 删除作业情况
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:ass:remove')")
    @Log(title = "作业情况", businessType = BusinessType.DELETE)
	@DeleteMapping("/{submissionIds}")
    public AjaxResult remove(@PathVariable Long[] submissionIds)
    {
        return toAjax(assService.deleteAssBySubmissionIds(submissionIds));
    }


    @GetMapping("/stats")
    public ResponseEntity<HomeworkStatsVO> getHomeworkStats() {
        HomeworkStatsVO stats = assService.getHomeworkSubmissionStats();
        return ResponseEntity.ok(stats);
    }
}
